package workflow.dao;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import org.springside.core.dao.HibernateEntityDao;
import org.hibernate.Query;

import workflow.model.ProcessTransition;

import common.PageInfo;

public class ProcessTransitionDao extends HibernateEntityDao<ProcessTransition>{
	public List<ProcessTransition> findList(HashMap<String, String> hm, PageInfo pageInfo){
		try{
		
		String listHql = "FROM ProcessTransition po WHERE (1=1) ";
		String countHql = null;
		/*------------查询条件-------------*/
		//注意以设值方式组装查询条�?,参�?�PageItemManager 方法:findList
		
		String id = (String)hm.get("id");
		String name = (String)hm.get("name");
		String proInsId = (String)hm.get("proInsId");
		String startTaskFlag = (String)hm.get("startTaskFlag");
		String endTaskFlag = (String)hm.get("endTaskFlag");
		String status = (String)hm.get("status");


		int count[] = {0, 0, 0, 0, 0, 0};

		if(id != null && !"".equals(id.trim())){
			listHql += " AND id =:id";
			count[0] = 1;
		}

		if(name != null && !"".equals(name.trim())){
			listHql += " AND name LIKE:name";
			count[1] = 1;
		}

		if(proInsId != null && !"".equals(proInsId.trim())){
			listHql += " AND proInsId =:proInsId";
			count[2] = 1;
		}

		if(startTaskFlag != null && !"".equals(startTaskFlag.trim())){
			listHql += " AND startTaskFlag LIKE:startTaskFlag";
			count[3] = 1;
		}

		if(endTaskFlag != null && !"".equals(endTaskFlag.trim())){
			listHql += " AND endTaskFlag LIKE:endTaskFlag";
			count[4] = 1;
		}

		if(status != null && !"".equals(status.trim())){
			listHql += " AND status LIKE:status";
			count[5] = 1;
		}

		listHql += " ORDER BY po.id DESC";
		countHql = listHql;
		countHql = countHql.replace("FROM ProcessTransition", "SELECT COUNT(*) FROM ProcessTransition");
		Query queryCount = this.createQuery(countHql);
		Query queryList = this.createQuery(listHql);

		if(count[0] == 1){
			queryList.setLong("id", Long.valueOf(id));
			queryCount.setLong("id", Long.valueOf(id));
		}

		if(count[1] == 1){
			queryList.setString("name", "%" + name + "%");
			queryCount.setString("name", "%" + name + "%");
		}

		if(count[2] == 1){
			queryList.setLong("proInsId", Long.valueOf(proInsId));
			queryCount.setLong("proInsId", Long.valueOf(proInsId));
		}

		if(count[3] == 1){
			queryList.setString("startTaskFlag", "%" + startTaskFlag + "%");
			queryCount.setString("startTaskFlag", "%" + startTaskFlag + "%");
		}

		if(count[4] == 1){
			queryList.setString("endTaskFlag", "%" + endTaskFlag + "%");
			queryCount.setString("endTaskFlag", "%" + endTaskFlag + "%");
		}

		if(count[5] == 1){
			queryList.setString("status", "%" + status + "%");
			queryCount.setString("status", "%" + status + "%");
		}



		//分页查询-START
		List countList = queryCount.list();
		int totalSize = 0;//总数
		if(countList.size() > 0){
			totalSize = Integer.valueOf(countList.get(0).toString());
			pageInfo.setTotalSize(totalSize);
		} else {
			pageInfo.setTotalSize(0);
		}
		
		queryList.setMaxResults(pageInfo.getPageSize());//设置查询数量
		queryList.setFirstResult(pageInfo.getPageNO() * pageInfo.getPageSize());//设置查询分段
		//分页查询-END
		
		return queryList.list();
		
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<ProcessTransition>();
		}
	}
	
	public boolean deleteByIds(String ids){
		boolean isSuccessful = false;
		try{			
			String hql = "DELETE FROM ProcessTransition po WHERE po.id IN (" + ids + ")";
			this.getHibernateTemplate().bulkUpdate(hql);
			isSuccessful = true;
		} catch (Exception e){
			e.printStackTrace();
		}
		
		return isSuccessful;
	}
}
